/*region 规则列表相关*/
function RuleViewer(taskId) {
    var ruleStatistic = new RuleStatistic();

    var baseUrl = "/m/rule";
    var taskId = taskId;
    var ruleTypeArr = [
        "unschedule_grade", "年级不排课",
        "unschedule_class", "班级不排课",
        "unschedule_course", "课程不排课",
        "unschedule_course_grade", "年级课程不排课",
        "unschedule_teacher", "教师不排课",
        "preschedule", "预排",
        "importance_japq", "教案平齐",
        "importance_zrkfb", "周任课分布",
        "importance_rrkfb", "日任课分布",
        "importance_lsxz", "连上限制"
    ];

    // 可以删除的
    var canDeletedArr = [
        "unschedule_grade",
        "unschedule_class",
        "unschedule_course",
        "unschedule_course_grade",
        "unschedule_teacher"];

    // 重要性
    var importanceArr = [
        "9", "必须保证",
        "5", "特别重要",
        "3", "相对重要",
        "1", "一般"];

    var isDeleted = false;

    // 委托删除事件
    $("body").delegate("a[name='ruleViewDeleteRule']", "click", deleteRule);

    // 委托修改事件
    $("body").delegate("a[name='ruleViewEditRule']", "click", editRule);

    // 设置任务Id
    this.setTaskId = function (id) {
        taskId = id
    }

    /*显示规则列表模态框*/
    this.getRuleList = function (deleteEnabled) {
        //如果有规则没保存先保存
        // generateResult();
        //展示
        var unScheduleRuleType = $("#unScheduleRuleListTypeSlt").val();
        $("#ruleListModal").modal('show');

        // 统计结果
        ruleStatistic.get(taskId, function (ruleStatistics) {
            var totalCount = ruleStatistics.getTotalCount();

            var content = "总计" + totalCount + "条规则，其中" +
                "“必须保证”" + ruleStatistics.requiredTotalCount + "条；" +
                "“特别重要”" + ruleStatistics.veryImportantTotalCount + "条；" +
                "“相对重要”" + ruleStatistics.importantTotalCount + "条；" +
                "“一般”" + ruleStatistics.normalTotalCount + "条";

            $("#ruleStatistics").text(content);
        })


        $("#ruleListTable_body").html("");
        jQuery.ajax({
            type: "GET",
            url: baseUrl + "/getRuleList?taskId=" + taskId + "&ruleListType=" + unScheduleRuleType,
            async: false,
            success: function (msg) {
                var message = JSON.parse(msg);
                var retData = message.data;
                if (retData != null) {
                    var length = retData.length;
                    var tableBodyHtml = new Array();
                    for (var i = 0; i < length; i++) {
                        deleteEnabled = false;

                        var ruleId = retData[i].ruleItemId;
                        var ruleType = retData[i].ruleType;
                        var ruleListType = retData[i].type;
                        var content = retData[i].ruleContent;
                        var importanceValue = retData[i].ruleImportance;

                        tableBodyHtml.push("<tr id='td" + ruleId + "'>");
                        tableBodyHtml.push("<td>" + getRuleTypeNam(ruleListType) + "</td>");
                        tableBodyHtml.push("<td>" + content + "</td>");
                        tableBodyHtml.push("<td>" + getImportanceNam(importanceValue) + "</td>");

                        try {
                            if (canDeletedArr.contains(ruleListType)) {
                                deleteEnabled = true;
                            }

                            if (isMyTask) {
                                if (deleteEnabled) {
                                    tableBodyHtml.push(
                                        "<td>" +
                                        "<a href='javascript:void(0);' name='ruleViewDeleteRule' ruleType='" + ruleType + "' ruleItemId='" + retData[i].ruleItemId + "' >删除</a>" +
                                        "</td>");
                                } else {
                                    tableBodyHtml.push("<td>" +
                                        "<a href='javascript:void(0);' name='ruleViewEditRule' ruleType='" + ruleType + "' ruleListType='" + ruleListType + "' ruleItemId='" + retData[i].ruleItemId + "' >修改</a>" +
                                        "</td>");
                                }
                            }
                        } catch (e) {

                        }
                        tableBodyHtml.push("</tr>");
                    }
                    $("#ruleListTable_body").html(tableBodyHtml.join(""));
                }
            }
        });
    }

    function getRuleTypeNam(ruleType) {
        for (var i = 0; i < ruleTypeArr.length; i = i + 2) {
            var rt = ruleTypeArr[i];
            var name = ruleTypeArr[i + 1];
            if (rt == ruleType) {
                return name;
            }
        }

        return "";
    }

    function getImportanceNam(value) {
        for (var i = 0; i < importanceArr.length; i = i + 2) {
            var v = importanceArr[i];
            var name = importanceArr[i + 1];
            if (v == value) {
                return name;
            }
        }

        return "";
    }

    /*点击规则列表删除规则*/
    function deleteRule() {
        if (!confirm("确定删除吗？")) {
            return;
        }

        var $the = $(this);
        var ruleType = $the.attr("ruleType");
        var ruleItemId = $the.attr("ruleItemId");

        var postData = {"taskId": taskId, "unScheduleType": ruleType, "ruleItemId": ruleItemId};
        jQuery.ajax({
            type: "POST",
            url: baseUrl + "/deleteRule",
            async: false,
            data: postData,
            success: function (msg) {
                var retData = JSON.parse(msg);
                if (retData == true) {
                    //控制样式上不显示
                    $("#td" + ruleItemId).hide();
                    isDeleted = true;
                }
            }
        });

        //删除规则成功之后 需要删除tag里面相应的部分
        if (isDeleted == true) {
            jQuery.ajax({
                type: "POST",
                url: "/m/tagRule/deleteTagRule",
                async: false,
                data: postData,
                success: function (msg) {
                    var retDelTag = JSON.parse(msg);
                }
            });
        }
    }

    // 修改规则
    function editRule() {
        var the = $(this);
        var ruleListType = the.attr("ruleListType");

        if (ruleListType == "importance_japq") {
            location.href = "/m/rule/japq?taskId=" + taskId;
            return;
        }

        if (ruleListType == "importance_zrkfb") {
            location.href = "/m/rule/zrkfb?taskId=" + taskId;
            return;
        }

        if (ruleListType == "importance_rrkfb") {
            location.href = "/m/rule/rrkfb?taskId=" + taskId;
            return;
        }

        if (ruleListType == "importance_lsxz") {
            location.href = "/m/rule/lsxz?taskId=" + taskId;
            return;
        }

        if (ruleListType == "preschedule") {
            location.href = "/m/courseplan/preschedule?taskId=" + taskId;
            return;
        }

    }

    /*规则列表框关闭事件*/
    $('#ruleListModal').on('hidden.bs.modal', function () {
        //如果删除过规则，则重新加载页面，刷新数据
        if (isDeleted == true) {
            location.reload();
        }
    })

    // 添加、复用弹出窗口居中
    $('#ruleListModal').on('show.bs.modal', function (e) {
        $(this).find('.modal-dialog').css({
            'margin-top': function () {
                var modalHeight = 540; // $('#myModal').find('.modal-dialog').height();
                return ($(window).height() / 2 - (modalHeight / 2));
            }
        });
    });

    $("body").delegate("a[name='ruleViewDeleteRule']", "click", deleteRule);

    /*endregion 规则列表相关*/
}